package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult findList(WmSensitiveDto dto) {

        //分页参数检查
        dto.checkParam();

        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = Wrappers.<WmSensitive>lambdaQuery();

        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        IPage page = new Page(dto.getPage(), dto.getSize());
        page = page(page, lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult saveChannel(WmSensitiveDto dto) {
        if(dto ==  null || StringUtils.isBlank(dto.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        Integer count = wmSensitiveMapper.selectCount(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));

        if(count > 0){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词已经存在");
        }

        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto,wmSensitive);
        save(wmSensitive);
        return ResponseResult.okResult(wmSensitive);
    }

    @Override
    public ResponseResult updateChannel(WmSensitiveDto dto) {
        if(dto ==  null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto,wmSensitive);

        updateById(wmSensitive);

        return ResponseResult.okResult(wmSensitive.getId());
    }

    @Override
    public ResponseResult del(int id) {
        if(id <= 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitive = wmSensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getId, id));

        if(wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


        removeById(id);
        return ResponseResult.okResult(id);
    }
}
